#include "viconmodel.h"

VICONModel::VICONModel(QObject *parent) :
    BaseModel(parent)
{
    // Initialize member variables

    _subjectX = 0;
    _subjectY = 0;
    _subjectZ = 0;
    _subjectXVelocity = 0;
    _subjectYVelocity = 0;
    _subjectZVelocity = 0;

    _subjectRoll = 0;
    _subjectPitch = 0;
    _subjectYaw = 0;
    _subjectRollRate = 0;
    _subjectPitchRate = 0;
    _subjectYawRate = 0;

    _connectionObj = new VICONConnection();
}

/*
  toString

  Desc - Returns a string representation of the state of the model
  */
QString VICONModel::toString()
{
    QString outputString;

    outputString = QString::number(_subjectX);
    outputString += ",";
    outputString += QString::number(_subjectY);
    outputString += ",";
    outputString += QString::number(_subjectZ);
    outputString += ",";
    outputString += QString::number(_subjectXVelocity);
    outputString += ",";
    outputString += QString::number(_subjectYVelocity);
    outputString += ",";
    outputString += QString::number(_subjectZVelocity);
    outputString += ",/n";

    outputString += QString::number(_subjectRoll);
    outputString += ",";
    outputString += QString::number(_subjectPitch);
    outputString += ",";
    outputString += QString::number(_subjectYaw);
    outputString += ",";
    outputString += QString::number(_subjectRollRate);
    outputString += ",";
    outputString += QString::number(_subjectPitchRate);
    outputString += ",";
    outputString += QString::number(_subjectYawRate);

    return outputString;
}

/*
  getIPAddress

  Desc - Returns the IP address of the connection within the model
  */
int *VICONModel::getIPAddress()
{
    return _connectionObj->getIPAddress();
}

/*
  createConnection

  Desc - Creates a new connection object within the model
  */
void VICONModel::createConnection()
{
    //TODO
}

/*
  destroyConnection

  Desc - Destroys the connection object within the model
  */
void VICONModel::destroyConnection()
{
    //TODO
}

/*
  getConnection

  Desc - Returns a reference to the connection object
  */
VICONConnection *VICONModel::getConnection()
{
    return _connectionObj;
}

/*
  getDataForArduino

  Desc - Returns a string containing all the data that needs to be sent to the Arduino (includes X, Y, Z and Yaw)
  */
QString VICONModel::getDataForArduino()
{
    QString outputString;

    outputString = QString::number(_subjectX);
    outputString += ",";
    outputString += QString::number(_subjectY);
    outputString += ",";
    outputString += QString::number(_subjectZ);
    outputString += ",";
    outputString += QString::number(_subjectYaw);

    return outputString;
}


// slots

/*
  processData

  Desc - Processes a string of data
  */
void VICONModel::processData(QString incomingData)
{
    //TODO
}

/*
  changeIPAddress

  Desc - Stops the connection, changes the IP address, and starts the connection
         again
  */
void VICONModel::changeIPAddress(int *newIPAddress)
{
    _connectionObj->endConnection();

    _connectionObj->setIPAddress(newIPAddress);

    _connectionObj->startConnection();

    emit ipAddressHasBeenChanged();
}
